function createAccount() {
    var username = $('#username').val()
    var password = $('#password').val()
    console.log('username: ', username, ', password: ', password)
    $.post('/api/account', { username: username, password: password }, function(data) {
        console.log(data)
        $('#accountCreatedInfo').html(data.address)
    })
}

function accountList() {
    console.log('accountList')
    $.get('/api/accounts', function(data) {
        console.log(data)
        let html = ''
        for (let account of data.accounts) {
            html += '<li>' + '(balance: ' + account.balance + ')' + account.address + '</li>'
        }
        $('#accountList').html(html)
    })
}

function submit() {
    var file = document.getElementById('file').files[0]

    if (file) {
        var owner = $('#owner').val()
        var ownerAddress = $('#ownerAddress').val()
        if (owner == '' || ownerAddress == '') {
            alert('请录入拥有者名称和账户地址')
        } else {
            var reader = new FileReader()
            reader.onload = function(event) {
                var hash = sha1(event.target.result)
                $('#txInfo').html('文件hash已发送')
                $.get('/api/submit?hash=' + hash + '&owner=' + owner + '&ownerAddress=' + ownerAddress, function(data) {
                    if (data == 'Error') {
                        $('#submittedTxInfo').text('An error occured.')
                    } else {
                        $('#submittedTxInfo').html('Transaction hash: ' + data)
                    }
                })
            }
            reader.readAsArrayBuffer(file)
        }
    } else {
        alert('Please select a file')
    }
}

function getFileInfo() {
    var file = document.getElementById('file').files[0]
    if (file) {
        var reader = new FileReader()
        reader.onload = function(event) {
            var hash = sha1(event.target.result)
            $.get('/api/file?hash=' + hash + '&account=' + $('#readerAddress').val(), function(data) {
                if (data[0] == 0 && data[1] == '') {
                    $('#fileInfo').html('File not found')
                } else {
                    $('#fileInfo').html('Timestamp: ' + data[0] + ' Owner: ' + data[1])
                }
            })
        }
        reader.readAsArrayBuffer(file)
    } else {
        alert('Please select a file')
    }
}

function getTransactionInfo() {
    $.get('/api/transaction?txId=' + $('#txAddress').val(), function(data) {
        if (data) {
            $('#txInfo').html(JSON.stringify(data))
        } else {
            $('#txInfo').html('No transaction')
        }
    })
}

function transactionList() {
    $.get('/api/transactions', function(data) {
        if (data) {
            var liList = ''
            for (var i = 0; i < data.length; ++i) {
                liList += '<li>' + JSON.stringify(data[i]) + '</li>'
            }
            $('#events_list').html(liList)
        } else {
            $('#events_list').html('无完成的事务')
        }
    })
}

var socket = io('http://127.0.0.1:8080')

socket.on('connect', function() {
    socket.on('message', function(msg) {
        if (msg.type === 'file') {
            transactionList()
        }
    })
})
